//
//  UserInfoTableViewCell.m
//  Findif
//
//  Created by appdev on 2018/10/29.
//  Copyright © 2018 Cicel Chen. All rights reserved.
//

#import "UserInfoTableViewCell.h"

@interface UserInfoTableViewCell ()<UITextViewDelegate,UITextFieldDelegate>

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, strong)UILabel * nameLabel;
@property (nonatomic, strong)UILabel * genderLabel;
@property (nonatomic, strong)UILabel * brithdayLabel;
@property (nonatomic, weak)UIImageView * rightImageView;
@property (nonatomic, weak)UILabel * exploreLabel;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UIView * threeLineView;
@property (nonatomic, assign)int maxCount;

@end

@implementation UserInfoTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    self.maxCount = 40;
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor clearColor];
    
    UIView * backView = [UIView new];
    
    backView.backgroundColor = ZRWhiteColor;
    backView.userInteractionEnabled = YES;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    
    self.backView = backView;
    
    [self.contentView addSubview:self.backView];
    
    self.nameLabel = [self setupLabelWithTitle:@"昵称"];
    [self.backView addSubview:self.nameLabel];
    self.genderLabel = [self setupLabelWithTitle:@"性别"];
    [self.backView addSubview:self.genderLabel];
    self.brithdayLabel = [self setupLabelWithTitle:@"年龄"];
    [self.backView addSubview:self.brithdayLabel];
    
    self.oneLineView = [self setupLineView];
    [self.backView addSubview:self.oneLineView];
    self.twoLineView = [self setupLineView];
    [self.backView addSubview:self.twoLineView];
    self.threeLineView = [self setupLineView];
    [self.backView addSubview:self.threeLineView];
    

    
    self.nameTextFiled = [self setupTextFiledWithTitle:@"请输入您修改的昵称"];
    [self.backView addSubview:self.nameTextFiled];
    
    self.genderButton = [self setupButtonWithTitle:@"请选择性别"];
    [self.backView addSubview:self.genderButton];
    self.brithdayButton = [self setupButtonWithTitle:@"未填写"];
    [self.backView addSubview:self.brithdayButton];
    
    UIImageView * rightImageView = [UIImageView new];
    
    rightImageView.image = [UIImage imageNamed:@"右箭头"];
    
    self.rightImageView = rightImageView;
    
    [self.backView addSubview:self.rightImageView];
    
    UILabel * exploreLabel = [UILabel new];
    
    exploreLabel.text = @"个性\n签名";
    exploreLabel.textColor = ZRBlackColor;
    exploreLabel.numberOfLines = 2;
    exploreLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    self.exploreLabel = exploreLabel;
    
    [self.backView addSubview:self.exploreLabel];
    
    UITextView * exploreFiled =[[UITextView alloc] init];
    
    exploreFiled.backgroundColor= ZRWhiteColor;
    exploreFiled.text = @"请输入你的个性签名....";
    exploreFiled.font = [UIFont systemFontOfSize:12 * SCREENH6];
    exploreFiled.textColor = [UIColor lightGrayColor];
    exploreFiled.delegate = self;
    exploreFiled.scrollEnabled = NO;
    
    self.exploreFiled = exploreFiled;
    [self.backView addSubview:self.exploreFiled];
}


- (void)layoutSubviews{
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView).offset(15 * SCREENH6);
        make.trailing.mas_equalTo(self.contentView).offset(-15 * SCREENH6);
        make.centerY.mas_equalTo(self.contentView);
        make.height.mas_equalTo(203 * SCREENH6);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.top.mas_equalTo(self.backView).offset(50 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(50 * SCREENH6);
    }];
    
    [self.threeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.twoLineView);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(50 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.backView).offset(15 * SCREENH6);
    }];
    
    [self.nameTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.nameLabel);
        make.leading.mas_equalTo(self.backView).offset(80 * SCREENH6);
        make.trailing.mas_equalTo(self.oneLineView);
        make.height.mas_equalTo(40 * SCREENH6);
    }];
    
    [self.exploreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.nameLabel);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(8 * SCREENH6);
    }];
    
    [self.exploreFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.nameTextFiled);
        make.height.mas_equalTo(48 * SCREENH6);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(1 * SCREENH6);
    }];
    
    [self.genderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.exploreLabel);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.genderButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.nameTextFiled);
        make.centerY.mas_equalTo(self.genderLabel);
    }];
    
    [self.brithdayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.genderLabel);
        make.top.mas_equalTo(self.threeLineView.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.brithdayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.genderButton);
        make.centerY.mas_equalTo(self.brithdayLabel);
    }];

}

- (UIButton *)setupButtonWithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [button setTitleColor:ZRBlackColor forState:UIControlStateSelected];
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    return button;
}


- (UITextField *)setupTextFiledWithTitle:(NSString *)title{
    
    UITextField * textFiled = [[UITextField alloc] init];
    
    textFiled.borderStyle = UITextBorderStyleNone;
    textFiled.backgroundColor = ZRWhiteColor;
    //    textFiled.layer.borderColor = [[UIColor clearColor] CGColor];
    //设置光标颜色
    textFiled.tintColor = ZRBlackColor;
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0xa5a5a5);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:title attributes:attrs];
    textFiled.textColor = ZRBlackColor;
    //    textFiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //    //设置显示模式为永远显示(默认不显示)
    //    textFiled.leftViewMode = UITextFieldViewModeAlways;
    textFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.delegate = self;
    
    return textFiled;
}


- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe8e8e8);
    
    return lineView;
}

- (UILabel *)setupLabelWithTitle:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.textColor = ZRBlackColor;
    label.text = title;
    label.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    return label;
}


- (void)clickButton:(UIButton *)button{
    
    NSString * name;
    
    if (button == self.genderButton) {
        
        name = @"gender";
    }else if (button == self.brithdayButton){
        
        name = @"brith";
    }
    if (self.userInfoTableViewCellDelegate && [self.userInfoTableViewCellDelegate respondsToSelector:@selector(clickItemButton:)])
    {
        [self.userInfoTableViewCellDelegate clickItemButton:name];
    }
}


#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    
    if(textView.text.length < 1){
        textView.text = @"请输入你的个性签名....";
        textView.textColor = [UIColor lightGrayColor];
    }else{
        
        [[ZRNetworkTool sharedTool] updateUserInfoWithUpdate_Class:@"personality_sign" WithUpdate_content:textView.text WithBlock:^(BOOL success, id responseData) {
            
            if (success) {
            }
        }];
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@"请输入你的个性签名...."]){
        textView.text=@"";
        textView.textColor=[UIColor blackColor];
    }
}


-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //如果是删除减少字数，都返回允许修改
    if ([text isEqualToString:@""]) {
        return YES;
    }
    if (range.location>= self.maxCount)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}


- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (textField == self.nameTextFiled) {
        
        [[ZRNetworkTool sharedTool] updateUserInfoWithUpdate_Class:@"user_name" WithUpdate_content:textField.text WithBlock:^(BOOL success, id responseData) {
            
            if (success) {
                
            }
        }];
    }
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
